package name.williamwall.buxoid;

/**
 * This software is made available under the Gnu General Public License v3
 * and has absolutely no waranty. You may change the software to suit your needs.
 * 
 * Author: William Wall, IV
 */
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class AccountList extends ListActivity {
	// menu ids
	private static final int UPDATE_ID = Menu.FIRST;
	private static final int SETTINGS_ID = Menu.FIRST + 1;
	private static final int HELP_ID = Menu.FIRST + 2;
	private int mBalanceType;
	private Button mVideosButton;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.list_accounts);
        
        mVideosButton = (Button) findViewById(R.id.button_videos);
        mVideosButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent(AccountList.this, HelpVideos.class));
            }
        });
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		fillData();
	}

	private void fillData() {
	    try {
    		setTitle("Buxoid: Accounts");
        	mBalanceType = Integer.parseInt(Buxoid.mDb.getSetting(BuxoidDb.SETTING_BALANCE_TYPE));
        	
        	String[] from = new String[] {
        			BuxoidDb.ACCOUNT_NAME,
        			BuxoidDb.ACCOUNT_BALANCE,
        			BuxoidDb.ACCOUNT_LAST_SYNC
        	};
        	
        	int[] to = new int[] {
        			R.id.row_account_name,
        			R.id.row_account_balance,
        			R.id.row_account_sync
        	};
        	
        	// Get all of the accounts from the database and create the item list
        	Cursor c = Buxoid.mDb.fetchAllAccounts();
        	startManagingCursor(c);
    
    		AccountAdapter adapter = new AccountAdapter(this, R.layout.row_account, c, from, to, mBalanceType);
    		setListAdapter(adapter);
    		
    		if (c.getCount() == 0) {
    		    mVideosButton.setVisibility(View.VISIBLE);
    		} else {
    		    mVideosButton.setVisibility(View.GONE);
    		}
	    } catch (IllegalStateException e) {
	        // this happens when the user starts an update and navigates away from the app
	        // so just ignore it
	    }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem item;
        // set update menu item and icon
        item = menu.add(0, UPDATE_ID, 1, R.string.menu_update);
        item.setIcon(android.R.drawable.ic_menu_set_as);
        
        // set settings menu item and icon
        item = menu.add(0, SETTINGS_ID, 2, R.string.menu_settings);
        item.setIcon(android.R.drawable.ic_menu_preferences);
        
        // set help menu item and icon
        item = menu.add(0, HELP_ID, 3, R.string.menu_help);
        item.setIcon(android.R.drawable.ic_menu_help);
        
        return true;
    }

    @Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	Intent i;
        switch(item.getItemId()) {
        case UPDATE_ID:
            Buxoid.mUICallback = new Handler();
            Buxoid.mFinish = new Runnable () {
                public void run () {
                    fillData();
                }
            };
            Buxoid.update(this);
            return true;
        case SETTINGS_ID:
        	i = new Intent(this, EditSettings.class);
            startActivity(i);
            return true;
        case HELP_ID:
            i = new Intent(this, Help.class);
            i.putExtra("topic", "Accounts");
            this.startActivity(i);
            return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
	}

	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Buxoid parent = (Buxoid) this.getParent();
        Buxoid.mViewAccountId = id;
        parent.getTabHost().setCurrentTabByTag("transactions");
    }	
}
